package com.laviedesfons.jhmtasc.types.abstracts;

import com.laviedesfons.jhmtasc.exceptions.MException;
import com.laviedesfons.jhmtasc.types.interfaces.InternalType;
import fr.x9c.cadmium.kernel.Block;
import fr.x9c.cadmium.kernel.Value;

/**
 *
 * @author Romano
 */
public abstract class WrappedType<T extends InternalType> extends AbstractType<T> {

    public WrappedType(T t) {
        wrapped = t;
    }

    public WrappedType() {
        if(getMetaModel().isGeneric()
                && getMetaModel().getParams().length == 1){
            wrapped = (T) getMetaModel().getParams()[0].create();
        }
    }

    @Override
    public Value toValue() {
        return Value.createFromBlock(
                Block.createBlock(getParentId(), wrapped.toValue()));
    }

    @Override
    public WrappedType<T> fromValue(Value v) throws MException {
        if (v.isBlock() && v.asBlock().sizeValues() == 1) {
            v = v.asBlock().get(0);
        }
        wrapped.fromValue(v);
        return this;
    }
}
